#include "qtMessage.h"
#include "picPublish.h"
#include <multi_select.h>
#include <stdio.h>
#include <stdbool.h>
#include <cJSON.h>
#include <list.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <devnode.h>

void qtMessageHandle(msgbuf_t *msg)
{
	int cmdtype;
	int value;
	struct qt_data_downblock *data;

	data = (struct qt_data_downblock *)msg;
	cmdtype = data->head.cmdtype;
	value = data->value;
	
	if (cmdtype == MSG_TYPE_RECORD_REQ)
	{
		//DEBUG("qt msg cmdtype:%02x value:%02x", cmdtype, value);
        if (value == 1)
		{
			DEBUG("recordWorkStart devSn:%s", data->devSn);
            recordWorkStart(data->devSn);					//开始录音
		}
		else
		{
			if (value == 3)
			{
				//确定播报
				DEBUG("broadcast_confirm devSn:%s", data->devSn);
				voice_broadcast_confirm(data->devSn);
			}
			else if (value == 4)
			{
				//取消播报
				DEBUG("broadcast_cancel");
				voice_broadcast_cancel(data->devSn);
			}
			else
			{
				DEBUG("recordWorkStop");
				recordWorkStop();							//停止录音
			}
		}
    } 
	else if (cmdtype == MSG_TYPE_WARNLEVEL)
	{
   		//注意安全 准备转移 立即转移	消除警报
		if(update_warn_status(value) == 0)
		{
			//QT界面下发请求
			warn_handler_start();
		}
    }
	else if (cmdtype == MSG_TYPE_GET_ONLINE)
	{
		//开机广播获取终端在线状态
		start_scan_online_thread();
	}
	else if (cmdtype == MSG_TYPE_STOP_BROADCAST)
	{
		//收到按钮 取消语音广播
		stop_voice_broadcast();
	}
	else if(cmdtype == MSG_TYPE_ON_DEMAND)
	{
		//点播
		if(ALARM_STATE_Status == data->command)
		{
			//在线点播
			oline_unicast_of_devSn(data->devSn);
		}
		else
		{
			//报警点播
			warn_unicast_of_devSn(data->devSn, data->command);
		}
	}
	else if(cmdtype == MSG_TYPE_GW_STATUS)
	{
		get_all_status_info();
	}
	else if(cmdtype == MSG_TYPE_PLAY_DEF_WAV)
	{
		myexec("killall aplay", NULL, NULL);
		myexec("aplay /userdata/res/audio/be_careful_8000.wav &", NULL, NULL);
	}
	else if(cmdtype == MSG_TYPE_RECORD_WAV)
	{
		myexec("killall arecord", NULL, NULL);
		myexec("arecord -d 3 -r 24000 -c 1 -f S16_LE /userdata/res/audio/out_24khz_1ch.wav &", NULL, NULL);
	}
	else if(cmdtype == MSG_TYPE_PLAY_RECORD_WAV)
	{
		myexec("killall aplay", NULL, NULL);
		myexec("aplay /userdata/res/audio/out_24khz_1ch.wav &", NULL, NULL);
	}
	else if(cmdtype == MSG_TYPE_SEND_BROADCAST_TASK)
	{
		if(data->command == 1)
		{
			//更新广播图片 或 语音
			start_broadcast_task(data);
		}
		else
		{
			//广播其它操作:恢复默认 播放 停止
			handle_broadcast_task(data);
		}
	}
}


